package com.sf.shiva.trtms.ground.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 异常转换工具
 *
 */
public class ExceptionUtils {

    private ExceptionUtils() {
    }

    public static StringBuffer toString(Exception e, int msgLength) {
        StringWriter sw = null;
        PrintWriter pw = null;
        StringBuffer msg;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw, true);
            e.printStackTrace(pw);
            msg = sw.getBuffer();
        } catch (Exception e2) {
            msg = new StringBuffer("ExceptionUtils.toString");
            logger().error("toString", e2);
        } finally {
            IOUtils.close(pw, sw);
        }
        msg.setLength(msgLength);
        return msg;
    }

    private static Logger logger() {
        return LoggerFactory.getLogger(ExceptionUtils.class);
    }

    /**
     * 默认截取4000
     *
     * @param e
     * @return
     */
    public static String toString(Exception e) {
        return toString(e, 4000).toString();
    }

    /**
     * 抓取异常信息
     *
     * @param throwable--异常对象
     * @return
     */
    public static String getExceptionMessage(Throwable throwable) {
        String clsname = throwable.getClass().getSimpleName();
        if (clsname.contains("Novatar") || clsname.contains("NullPointer") || clsname.contains("Runtime")) {
            String invalidMessage = throwable.getMessage();
            if (null != invalidMessage && !"".equals(invalidMessage)) {
                return invalidMessage;
            }
        }

        String result = "";
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            throwable.printStackTrace(pw);
            result = sw.toString();
            int index = result.indexOf("Caused by:");
            if (index > 0) {
                result = result.substring(index);
            } else {
                int pos = result.indexOf("at ");
                if (pos > 0) {
                    result = result.substring(0, pos);
                }
            }
            result = result.replaceAll("Caused by:", "");
            if (result.length() > 4000) {
                result = result.substring(0, 4000);
            }
        } catch (Exception e) {
            logger().error("getExceptionMessage", e);
        } finally {
            IOUtils.close(pw, sw);

        }
        return result;
    }

    /**
     * 抓取异常信息
     *
     * @param throwable--异常对象
     * @return
     */
    public static String getExceptionDetailMessage(Throwable throwable) {
        String result = "";
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            throwable.printStackTrace(pw);
            result = sw.toString();
        } catch (Exception e) {
            logger().error("getExceptionDetailMessage", e);
        } finally {
            IOUtils.close(sw, pw);
        }
        return result;
    }
}
